package service;

import Dao.AdminDao;
import domain.Admin;

import java.io.IOException;
import java.sql.SQLException;

public class AdminService {
    private AdminDao adminDao = new AdminDao();
    //修改管理员密码的方法
    public boolean updateAdmin(String admin_name, String pwd) throws SQLException, IOException,
            ClassNotFoundException, NoSuchFieldException,
            InstantiationException, IllegalAccessException {
        if (AdminUnique(admin_name)) {
            System.out.println("管理员名称有误！");
            return false;
        } else if (Login(admin_name, pwd)) {
            System.out.println("密码不能重复！");
            return false;
        } else {
            int re = adminDao.update("update adminTable set password=md5(?)", pwd);
            //传入密码，对密码修改并进行加密处理
            return re > 0;
        }

    }

    //判断这个管理员的密码是不是唯一的，即输入管理员名称，密码查看是否存在这个对象
    public boolean Login(String admin_name, String pwd) throws SQLException, IOException,
            NoSuchFieldException, ClassNotFoundException,
            InstantiationException, IllegalAccessException {
        Admin admin = adminDao.querySingle("select * from adminTable where " +
                "admin_name=? and password=md5(?)", Admin.class, admin_name, pwd);
        if (admin == null) {
            return false;
        }
        return true;
    }
    private boolean AdminUnique(String admin_name) throws SQLException, IOException,
            NoSuchFieldException, ClassNotFoundException,
            InstantiationException, IllegalAccessException {
        Admin admin = adminDao.querySingle("select * from adminTable " +
                "where admin_name=?", Admin.class, admin_name);
        if (admin == null) {
            return true;
        }
        return false;
    }


}
